﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataObjectsPhone;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.Diagnostics;
using ee_wp_tourmaker.WebServices;
using System.Collections.ObjectModel;

namespace ee_wp_tourmaker.DataControllers
{
    public class ChallengeData
    {
        // web service to get data from
        private ChallengeServices webservice;

        private DetailedChallengeService detailedService;

        //lists of differents types of challenges
        private List<Challenge> activeChallenges;
        private List<Challenge> doneChallenges;
        private List<Challenge> webChallenges;

        //saves the data into these files
        private static String ACTIVE_FILE = "activechallengefile.xml";
        private static String DONE_FILE = "donechallengefile.xml";

        //testing!
        public ChallengeData(){
            activeChallenges = new List<Challenge>();
            doneChallenges = new List<Challenge>();
        }

        public void downloadDetailedForid(Guid id)
        {
            detailedService = new DetailedChallengeService();
            detailedService.downloadChallengeWithID(id);
        }





        public List<Challenge> ActiveChallenges
        {
            get 
            {
                if (activeChallenges == null)
                    activeChallenges = new List<Challenge>();
                return activeChallenges;
            }
            set { activeChallenges = value; }
        }

        public List<Challenge> DoneChallenges
        {
            get 
            {
                if (doneChallenges == null)
                    doneChallenges = new List<Challenge>();
                return doneChallenges; 
            }
            set { doneChallenges = value; }
        }

        public List<Challenge> WebChallenges
        {
            get
            {
                if (webChallenges == null)
                {
                    webservice = new ChallengeServices();
                    webservice.startDownloading();
                    return null;
                }
                return webChallenges;
            }
            set { webChallenges = value; }
        }

        public void LoadActiveChallenges()
        {
            TextReader reader = null;

            try
            {
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream file = isoStorage.OpenFile(ACTIVE_FILE, FileMode.OpenOrCreate);
                reader = new StreamReader(file);
                if (file.Length > 0)
                {
                    XmlSerializer xs = new XmlSerializer(typeof(List<Challenge>));
                    this.activeChallenges = xs.Deserialize(reader) as List<Challenge>;

                }
                reader.Close();
            }
            catch (IsolatedStorageException)
            {
                //means that we need to create a new user!
                Debug.WriteLine("failed to load activeChallenges!");

            }

            finally
            {

                if (reader != null) reader.Dispose();

            }
        }

        public void LoadDoneChallenges()
        {
            TextReader reader = null;

            try
            {
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream file = isoStorage.OpenFile(DONE_FILE, FileMode.OpenOrCreate);
                reader = new StreamReader(file);
                if (file.Length > 0)
                {
                    XmlSerializer xs = new XmlSerializer(typeof(List<Challenge>));
                    this.doneChallenges = xs.Deserialize(reader) as List<Challenge>;

                }
                reader.Close();
            }
            catch (IsolatedStorageException)
            {
                //means that we need to create a new user!
                Debug.WriteLine("failed to load doneChallenges!");

            }

            finally
            {

                if (reader != null) reader.Dispose();

            }
        }

        public void SaveActiveChallenges()
        {
            TextWriter writer = null;
            try
            {
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream file = isoStorage.OpenFile(ACTIVE_FILE, FileMode.Create, FileAccess.Write);
                writer = new StreamWriter(file);

                XmlSerializer xs = new XmlSerializer(typeof(List<Challenge>));
                try
                {
                    xs.Serialize(writer, this.activeChallenges);

                }
                catch (Exception) { }

                writer.Close();
            }
            catch (Exception)
            {

                Debug.WriteLine("failed to save activeChallenges!");
            }

            finally
            {

                if (writer != null) writer.Dispose();

            }
        }

        public void SaveDoneChallenges()
        {
            TextWriter writer = null;
            try
            {
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream file = isoStorage.OpenFile(DONE_FILE, FileMode.Create, FileAccess.Write);
                writer = new StreamWriter(file);

                XmlSerializer xs = new XmlSerializer(typeof(List<Challenge>));
                try
                {
                    xs.Serialize(writer, this.doneChallenges);

                }
                catch (Exception) { }

                writer.Close();
            }
            catch (Exception)
            {

                Debug.WriteLine("failed to save doneChallenges!");
            }

            finally
            {

                if (writer != null) writer.Dispose();

            }
        }

        //should send the web inf about the challenges done by the user to filter out for the requested services
        public void LoadWebChallengesForFilter()
        {
        }
    }
}
